
using System;
using System.Collections.Generic;
using WooEngine.Common;

namespace WooEngine.Register
{
    public interface IRegisterProvider
    {
        void Regist();
        void Remove(); 
    }


    public class RegisterManager : SingletonBehaviour<RegisterManager>
    {
#if ODIN_INSPECTOR
        [ShowInInspector, ShowIf("showOdinInfo"), DictionaryDrawerSettings(IsReadOnly = true, DisplayMode = DictionaryDisplayOptions.OneLine)]
#endif
        private readonly Dictionary<string, IRegisterProvider> m_dbProviders = new Dictionary<string, IRegisterProvider>();

        public T RegistProvider<T>(string tag) where T : IRegisterProvider
        {
            if (!m_dbProviders.TryGetValue(tag, out IRegisterProvider provider))
            {
                provider = (IRegisterProvider)System.Activator.CreateInstance(typeof(T));
                provider.Regist();
                m_dbProviders.Add(tag, provider);
            }
            return (T)provider;
        }

        public void RegistProvider(Type type,string tag) 
        {
            if (!m_dbProviders.TryGetValue(tag, out IRegisterProvider provider))
            {
                provider = (IRegisterProvider)System.Activator.CreateInstance(type);
                provider.Regist();
                m_dbProviders.Add(tag, provider);
            }
        }

        public void RegistProvider(string assemblyName, string typeName, string tag)
        {
            if (!m_dbProviders.TryGetValue(tag, out IRegisterProvider provider))
            {
                provider = (IRegisterProvider)System.Activator.CreateComInstanceFrom(assemblyName, typeName);
                provider.Regist();
                m_dbProviders.Add(tag, provider);
            }
        }

        public void RemoveProvider(string tag)
        {
            if (m_dbProviders.TryGetValue(tag, out IRegisterProvider provider))
            {
                provider.Remove();
                m_dbProviders.Remove(tag);
            }
        }
    }
}
